local DEBUG = false

if DEBUG then
    print("init")
end

local feederCtl = require("feederctl")
feederCtl.init(1000 * 3600, 7000)

--wifi.setmode(wifi.SOFTAP)
wifi.setmode(wifi.STATION)
wifi.sta.config({ssid="xiaobeidai", pwd="sdxili402"})
wifi.sta.connect()

require("httpServer")
if httpServer:isListening() then
    httpServer:close()
end
httpServer:listen(80)
-- Pages
httpServer:use("/", function(req, res)
    f = file.open("index.html", "r")
    s = file.read()
    f:close()
    s = string.gsub(s, "%$(%w+)", {times=feederCtl.getFeedTimes()})
    res:send(s)
end)
httpServer:use("/feed", function(req, res)
    feederCtl.feed()
    res:sendFile("feed.html")
end)
-- API
httpServer:use("/api/feed", function(req, res)
    feederCtl.feed()
    res:send("{\"ret\":0}")
end)
httpServer:use("/api/feedtimes", function(req, res)
    res:send(string.format("{\"ret\":0,\"times\":%d}", feederCtl.getFeedTimes()))
end)

if DEBUG then
    print("init finish")
end

